package com.example.petstoreservice.controller;

import com.example.petstoreservice.entity.Coupon;
import com.example.petstoreservice.entity.Result;
import com.example.petstoreservice.service.CouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/coupon")
public class CouponController {

    @Autowired
    private CouponService couponService;

    @GetMapping("/all")
    public Result<List<Coupon>> getAllCoupons() {
        Result<List<Coupon>> result = new Result<>();
        result.setData(couponService.getAllCoupons());
        return result;
    }

    @GetMapping("/user/{username}")
    public Result<List<Coupon>> getCouponsByUsername(@PathVariable String username) {
        Result<List<Coupon>> result = new Result<>();
        result.setData(couponService.getCouponsByUsername(username));
        return result;
    }

    @PostMapping("/add")
    public Result<?> insertCoupon(@RequestBody Coupon coupon) {
        if (couponService.insertCoupon(coupon)) {
            return Result.success("添加优惠券成功");
        } else {
            return Result.fail("添加优惠券失败");
        }
    }

    @PostMapping("/update")
    public Result<?> updateCoupon(@RequestBody Coupon coupon) {
        if (couponService.updateCoupon(coupon)) {
            return Result.success("更新优惠券成功");
        } else {
            return Result.fail("更新优惠券失败");
        }
    }

    @GetMapping("/delete/{couponId}")
    public Result<?> deleteCoupon(@PathVariable int couponId) {
        if (couponService.deleteCoupon(couponId)) {
            return Result.success("删除优惠券成功");
        } else {
            return Result.fail("删除优惠券失败");
        }
    }
}
